package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetMaintenancePageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetMaintenancePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetMaintenancePageLogic {
	return &AssetMaintenancePageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetMaintenancePage 分页查询维修申请表项
func (l *AssetMaintenancePageLogic) AssetMaintenancePage(in *property_service.AssetMaintenancePageReq) (*property_service.AssetMaintenancePageRes, error) {
	// 定义一个维修申请表模型对象切片用于存储查询结果
	var data []*model.AssetMaintenance
	// 获取维修申请表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetMaintenance
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.MaintenanceCode != "" {
			do = do.Where(m.MaintenanceCode.Like("%" + in.Where.MaintenanceCode + "%")) // 申请单号
		}
		if in.Where.Reporter != "" {
			do = do.Where(m.Reporter.Like("%" + in.Where.Reporter + "%")) // 报修人
		}
		if in.Where.ApplicantDept != "" {
			do = do.Where(m.ApplicantDept.Like("%" + in.Where.ApplicantDept + "%")) // 申请科室
		}
		if in.Where.ContactNumber != "" {
			do = do.Where(m.ContactNumber.Like("%" + in.Where.ContactNumber + "%")) // 联系电话
		}
		if in.Where.UrgencyLevel != "" {
			do = do.Where(m.UrgencyLevel.Like("%" + in.Where.UrgencyLevel + "%")) // 紧急程度
		}
		if in.Where.CardNumber != "" {
			do = do.Where(m.CardNumber.Like("%" + in.Where.CardNumber + "%")) // 卡片编号
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.FaultDescription != "" {
			do = do.Where(m.FaultDescription.Like("%" + in.Where.FaultDescription + "%")) // 故障说明
		}
		if in.Where.Attachment != "" {
			do = do.Where(m.Attachment.Like("%" + in.Where.Attachment + "%")) // 附件
		}
		if in.Where.MaintenanceStatus != "" {
			do = do.Where(m.MaintenanceStatus.Like("%" + in.Where.MaintenanceStatus + "%")) // 状态
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 状态名称
		}

		// 处理时间类型字段，进行精确查询
		if in.Where.MaintenanceDate != "" {
			do = do.Where(m.MaintenanceDate.Eq(BeanUtils.GetDateFormat(in.Where.MaintenanceDate))) // 申请日期
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.AssetID > 0 {
			do = do.Where(m.AssetID.Eq(in.Where.AssetID)) // 资产ID
		}
		if in.Where.ReporterID > 0 {
			do = do.Where(m.ReporterID.Eq(in.Where.ReporterID)) // 报修人ID
		}
		if in.Where.ApplicantDeptID > 0 {
			do = do.Where(m.ApplicantDeptID.Eq(in.Where.ApplicantDeptID)) // 申请科室ID
		}
		if in.Where.StatusID > 0 {
			do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 状态ID
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetMaintenance{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetMaintenancePageRes{
		Count: count,
		List:  list.([]*property_service.AssetMaintenance),
	}, nil
}
